#include "stdafx.h"
#include "wf_drawhatch.h"

#include "pf_snapper.h"
#include "pf_graphicview.h"
#include "pf_information.h"



WF_DrawHatch::WF_DrawHatch(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Hatch",
                           container, graphicView) {

    hatch = NULL;
}


QAction* WF_DrawHatch::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Hatch")
    QAction* action = new QAction(tr("&Hatch"),  NULL);
	action->setIcon(QIcon(":/extui/menuhatch.png"));
    //action->zetStatusTip(tr("Draw Hatches and Solid Fills"));
    return action;
}


void WF_DrawHatch::init(int status) {
    PF_ActionInterface::init(status);

    PF_Hatch tmp(container, data);
    tmp.setLayerToActive();
    tmp.setPenToActive();
    if (PF_DIALOGFACTORY->requestHatchDialog(&tmp)) {
        data = tmp.getData();
        trigger();
        finish();
        graphicView->redraw(LX_Define::RedrawDrawing); 

    } else {
        finish();
    }
}



void WF_DrawHatch::trigger() {

    PF_DEBUG->print("PF_ActionDrawHatch::trigger()");

    //if (pos.valid) {
    //deletePreview();
	PF_Entity* e;

	// deselect unhatchable entities:
    for (e=container->firstEntity(LX_Define::ResolveNone); e!=NULL;
            e=container->nextEntity(LX_Define::ResolveNone)) {
        if (e->isSelected() && 
		    (e->rtti()==LX_Define::EntityHatch ||
			 e->rtti()==LX_Define::EntityEllipse || e->rtti()==LX_Define::EntityPoint ||
			 e->rtti()==LX_Define::EntityText || 
			 PF_Information::isDimension(e->rtti()))) {
			e->setSelected(false);
        }
    }
    for (e=container->firstEntity(LX_Define::ResolveAll); e!=NULL;
            e=container->nextEntity(LX_Define::ResolveAll)) {
        if (e->isSelected() && 
		    (e->rtti()==LX_Define::EntityHatch ||
			 e->rtti()==LX_Define::EntityEllipse || e->rtti()==LX_Define::EntityPoint ||
			 e->rtti()==LX_Define::EntityText || 
			 PF_Information::isDimension(e->rtti()))) {
			e->setSelected(false);
        }
    }

	// look for selected contours:
    bool haveContour = false;
    for (e=container->firstEntity(LX_Define::ResolveAll); e!=NULL;
            e=container->nextEntity(LX_Define::ResolveAll)) {
        if (e->isSelected()) {
            haveContour = true;
        }
    }

    if (!haveContour) {
        std::cerr << "no contour selected\n";
        return;
    }

    hatch = new PF_Hatch(container, data);
    hatch->setLayerToActive();
    hatch->setPenToActive();
    PF_Container* loop = new PF_Container(hatch);
    loop->setPen(PF_Pen(LX_Define::FlagInvalid));

    // add selected contour:
    for (PF_Entity* e=container->firstEntity(LX_Define::ResolveAll); e!=NULL;
            e=container->nextEntity(LX_Define::ResolveAll)) {

        if (e->isSelected()) {
            e->setSelected(false);
			// entity is part of a complex entity (spline, polyline, ..):
			if (e->getParent()!=NULL && 
			    (e->getParent()->rtti()==LX_Define::EntitySpline ||
				 e->getParent()->rtti()==LX_Define::EntityPolyline)) {
				e->getParent()->setSelected(false);
			}
            PF_Entity* cp = e->clone();
            cp->setPen(PF_Pen(LX_Define::FlagInvalid));
            cp->reparent(loop);
            loop->addEntity(cp);
        }
    }

    hatch->addEntity(loop);
	if (hatch->validate()) {
		container->addEntity(hatch);

		if (document) {
			document->startUndoCycle();
			document->addUndoable(hatch);
			document->endUndoCycle();
		}
		hatch->update();

		graphicView->redraw(LX_Define::RedrawDrawing);

		PF_DIALOGFACTORY->commandMessage(tr("Hatch created successfully."));
	}
	else {
		delete hatch;
		hatch = NULL;
		PF_DIALOGFACTORY->commandMessage(tr("Invalid hatch area. Please check that "
		"the entities chosen form one or more closed contours."));
	}
    //}
}



void WF_DrawHatch::mouseMoveEvent(PF_MouseEvent*) {
    PF_DEBUG->print("PF_ActionDrawHatch::mouseMoveEvent begin");

    /*if (getStatus()==SetPos) {
        PF_Vector mouse = snapPoint(e);
        pos = mouse;


        deletePreview();
        if (hatch!=NULL && !hatch->isVisible()) {
            hatch->setVisible(true);
        }
        offset = PF_Vector(graphicView->toGuiDX(pos.x),
                           -graphicView->toGuiDY(pos.y));
        drawPreview();
}*/

    PF_DEBUG->print("PF_ActionDrawHatch::mouseMoveEvent end");
}



void WF_DrawHatch::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_Vector mouse = snapPoint(e);

        switch (getStatus()) {
        case ShowDialog:
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        //deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawHatch::updateMouseButtonHints() {
    PF_DIALOGFACTORY->updateMouseWidget("", "");
}



void WF_DrawHatch::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawHatch::updateToolBar() {
    PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
}


// EOF
